<template>
	<view class="comments-box">
		<view class="comment-header">
			<view class="comment-header__logo">
				<image :src="comments.author.avatar" mode="aspectFill"></image>
			</view>
			<view class="comment-header__info">
				<view class="title" v-if="!comments.is_reply">{{comments.author.author_name}}</view><!-- 作者名称 -->
				<view class="title" v-else>
					{{comments.author.author_name}} 
					<text class="reply-text">回复</text> 
					{{comments.to}}
				</view><!-- 作者名称 -->
				<view class="time">{{comments.create_time | format}}</view><!-- 时间 -->
			</view>
		</view>
		<view class="comments-content">
			<view>{{comments.comment_content}}</view>
			<view class="comments-info">
				<view class="comments-btn" @click="commentsReply({comments, is_reply:reply})">回复</view>
			</view>
			<!-- 递归组件 -->
			<view class="comments-reply" v-for="item,index in comments.replys" :key="index">
				<comment-box :reply="true" :comments="item" @reply="commentsReply"></comment-box>
			</view>
		</view>
	</view>
</template>

<script>
	import commentBox from '@/components/comment-box/comment-box.vue'
	import {dataFormat} from '@/utils/tools.js';
	export default {
		// 使用递归组件时需要给个name值
		name: 'comment-box',
		data() {
			return {
				
			}
		},
		props: {
			// 评论内容
			comments: {
				type: Object,
				default() {
					return {}
				}
			},
			reply: {
				type: Boolean,
				default: false
			}
		},
		methods: {
			// 回复评论
			commentsReply(comment) {
				// 为了区分是 主回复 还是 子回复
				if(comment.is_reply) {
					comment.comments.replay_id = comment.comments.comment_id
					comment.comments.comment_id = this.comments.comment_id
				}
				
				// console.log('123',comment)
				
				this.$emit('reply', comment)
			}
		},
		filters: {
			// 时间格式化
			format(originVal) {
				return dataFormat(originVal)
			}
		},
		components:{
			commentBox
		}
	}
</script>

<style scoped lang="scss">
	.comments-box{
		margin: 30upx 0;
		.comment-header{
			display: flex;
			align-items: center;
			.comment-header__logo{
				width: 60upx;
				height: 60rpx;
				flex-shrink: 0;
				overflow: hidden;
				border-radius: 10upx;
				image{
					width: 100%;
					height: 100%;
				}
			}
			.comment-header__info{
				display: flex;
				flex-direction: column;
				padding-left: 30upx;
				font-size: 24upx;
				justify-content: space-between;
				color: #999;
				line-height: 1;
				.title{
					margin-bottom: 20upx;
					font-size: 28upx;
					color: #333;
					.reply-text{
						margin: 0 20upx;
						font-weight: bold;
						color: #000;
					}
				}
			}
		}
		.comments-content{
			margin-top: 20upx;
			font-size: 28upx;
			color: #000;
			.comments-info{
				margin-top: 30upx;
				display: flex;
				.comments-btn{
					padding: 4upx 20upx;
					font-size: 24upx;
					color: #999;
					border-radius: 40upx;
					border: 1px solid #ccc;
				}
			}
			.comments-reply{
				display: flex;
				margin: 30upx 0;
				padding: 0 20upx;
				border: 1px solid #eee;
			}
		}
	}
</style>
